<template>
  <div class="sub-sort">
    <div class="sort">
      <a :class="{ active: sortParams.sortField === null }" href="javascript:;" @click="changeField(null)">
        默认排序
      </a>
      <a :class="{ active: sortParams.sortField === 'publishTime' }" href="javascript:;" @click="changeField('publishTime')">
        最新商品
      </a>
      <a :class="{ active: sortParams.sortField === 'orderNum' }" href="javascript:;" @click="changeField('orderNum')">
        最高人气
      </a>
      <a :class="{ active: sortParams.sortField === 'evaluateNum' }" href="javascript:;" @click="changeField('evaluateNum')">
        评论最多
      </a>
      <a href="javascript:;" @click="changeField('price')">
        价格排序
        <i class="arrow up" :class="{active:sortParams.sortField === 'price' && sortParams.sortMethod == 'asc'}"/>
        <i class="arrow down" :class="{active: sortParams.sortField === 'price' && sortParams.sortMethod == 'desc'}"/>
      </a>
    </div>
    <div class="check">
      <XtxCheckbox @change="change" v-model="sortParams.inventory">仅显示有货商品</XtxCheckbox>
      <XtxCheckbox @change="change" v-model="sortParams.onlyDiscount">
        仅显示特惠商品
      </XtxCheckbox>
    </div>
  </div>
</template>
<script>
import { reactive } from 'vue'
export default {
  name: 'SubSort',
  setup (props, { emit }) {
    const sortParams = reactive({
      // inventory 是否有库存
      inventory: false,
      // onlyDiscount 只显示特惠
      onlyDiscount: false,
      // sortField 排序字段 可选值：[publishTime,orderNum,price,evaluateNum] -[最新商品，最高人气，价格排序，评论最多]
      // publishTime:最新商品 orderNum: 最高人气 price:价格排序 evaluateNum：评论最多
      sortField: null,
      // sortMethod 排序规则  asc:升序  desc:降序
      sortMethod: null
    })

    const changeField = sortField => {
      if (sortField === 'price') {
        // 如果点击的价格
        sortParams.sortField = sortField
        // 修改sortMethod
        sortParams.sortMethod = sortParams.sortMethod === 'desc' ? 'asc' : 'desc'
      } else {
        // 不是price
        sortParams.sortField = sortField
        sortParams.sortMethod = null
      }
      emit('changeSort', sortParams)
    }

    const change = () => {
      emit('changeSort', sortParams)
    }

    return {
      sortParams,
      changeField,
      change
    }
  }
}
</script>
<style scoped lang='less'>
.sub-sort {
  height: 80px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  .sort {
    display: flex;
    a {
      height: 30px;
      line-height: 28px;
      border: 1px solid #e4e4e4;
      padding: 0 20px;
      margin-right: 20px;
      color: #999;
      border-radius: 2px;
      position: relative;
      transition: all .3s;
      &.active {
        background: @xtxColor;
        border-color: @xtxColor;
        color: #fff;
      }
      .arrow {
        position: absolute;
        border: 5px solid transparent;
        right: 8px;
        &.up {
          top: 3px;
          border-bottom-color: #bbb;
            &.active {
            border-bottom-color: @xtxColor;
          }
        }
        &.down {
          top: 15px;
          border-top-color: #bbb;
          &.active {
            border-top-color: @xtxColor;
          }
        }
      }
    }
  }
  .check {
    .xtx-checkbox {
      margin-left: 20px;
      color: #999;
    }
  }
}
</style>
